package com.csair.irp.job.file.util;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;

public class FileUtil {

    /**
     * 关闭流
     *
     * @param closeable
     */
    public static void close(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }

    /**
     * 压缩
     *
     * @param srcFile 源目录
     * @param dest    要压缩的目录
     * @throws ZipException 异常
     */
    public static File zip(File srcFile, String dest) throws ZipException {

        // 创建目标文件
        String destname = buildDestFileName(srcFile, dest);
        ZipParameters par = new ZipParameters();
        par.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        par.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

        // 设置密码
        par.setEncryptFiles(true);
        par.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
        par.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);

        ZipFile zipfile = new ZipFile(destname);
        if (srcFile.isDirectory()) {
            zipfile.addFolder(srcFile, par);
        } else {
            zipfile.addFile(srcFile, par);
        }
        return zipfile.getFile();
    }

    /**
     * 解压
     *
     * @param zipfile 压缩包文件
     * @param dest    目标文件
     * @throws ZipException 抛出异常
     */
    public static File unZip(File zipfile, String dest) throws ZipException {
        ZipFile zfile = new ZipFile(zipfile);

        // 在GBK系统中需要设置
        zfile.setFileNameCharset("UTF-8");
        if (!zfile.isValidZipFile()) {
            throw new ZipException("压缩文件不合法，可能已经损坏！");
        }

        String fileName = zipfile.getName().substring(0, zipfile.getName().length() - 4);

        File file = new File(dest, fileName);
        if (!file.exists() || !file.isDirectory()) {
            file.mkdirs();
        }

        zfile.extractAll(file.getAbsolutePath());
        return file;
    }

    public static boolean isZip(File file) {
        return file.getName().endsWith(".zip");
    }

    public static String buildDestFileName(File srcfile, String dest) {
        if (dest == null || dest.isEmpty()) {
            if (srcfile.isDirectory()) {
                dest = srcfile.getParent() + File.separator + srcfile.getName() + ".zip";
            } else {
                String filename = srcfile.getName().substring(0, srcfile.getName().lastIndexOf("."));
                dest = srcfile.getParent() + File.separator + filename + ".zip";
            }
        } else {

            createPath(dest);
            if (!dest.endsWith(File.separator)) {
                dest += File.separator;
            }
            String filename = "";
            if (srcfile.isDirectory()) {
                filename = srcfile.getName();
            } else {
                filename = srcfile.getName().substring(0, srcfile.getName().lastIndexOf("."));
            }
            dest += filename + ".zip";
        }
        return dest;
    }

    private static void createPath(String dest) {
        File destDir = null;
        if (dest.endsWith(File.separator)) {
            destDir = new File(dest);// 给出的是路径时
        } else {
            destDir = new File(dest.substring(0, dest.lastIndexOf(File.separator)));
        }

        if (!destDir.exists()) {
            destDir.mkdirs();
        }
    }
}
